<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // Set实现数据的并集和交集

      // let arr1 = [1, 2, 3]
      // let arr2 = [2, 3, 5]
      // const arr = new Set([...arr1, ...arr2])
      // console.log([...arr])

      // 交集
      // let arr1 = [1, 2, 3, 1]
      // let arr2 = [3, 4, 5]

      // const arr = [...new Set([...arr1, ...arr2])] // [1,2,3,4,5]
      // const newArr = []
      // arr.forEach((item) => {
      //   const newArr1 = arr1.filter((item1) => item1 === item)
      //   const newArr2 = arr2.filter((item2) => item2 === item)
      //   if (newArr1.length && newArr2.length) {
      //     if (newArr1.length <= newArr2.length) {
      //       newArr.push(...newArr1)
      //     } else {
      //       newArr.push(...newArr2)
      //     }
      //   }
      // })
      // console.log(newArr)

      // map 的key,value是任意的数据类型
      // 找出出现次数最多的元素==> Map方法

      const arr = [1, 2, 3, 3, 2, 1, 5, 5, 1]
      const map = new Map()
      // map里的元素不重复
      arr.forEach((item) => {
        const newArr = arr.filter((item1) => item1 === item)
        map.set(item, newArr.length)
      })
      let maxValue = 0
      let keyValue = 0
      for (let [key, value] of map) {
        if (value >= maxValue) {
          maxValue = value
          keyValue = key
        }
      }
      console.log('出现次数最多的是' + keyValue + '次数是' + maxValue)
    </script>
  </body>
</html>
